package com.sskj.xgja.verify;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.hjq.toast.ToastUtils;
import com.just.agentweb.AgentWeb;
import com.sskj.base.BuildConfig;
import com.sskj.xgja.MVPActivity;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.R;

import butterknife.BindView;

/**
 * 风险介绍
 *
 * @author Hey
 * Create at  2019/03/22
 */
public class IntroductionActivity extends MVPActivity<IntroductionPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.read_check)
    CheckBox readCheck;
    @BindView(R.id.risk_content)
    LinearLayout riskContent;
    @BindView(R.id.submit)
    Button submit;
    AgentWeb agentWeb;

    @Override
    public int getLayoutId() {
        return R.layout.activity_introduction;
    }

    @Override
    public IntroductionPresenter getPresenter() {
        return new IntroductionPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());
        agentWeb = AgentWeb.with(this)
                .setAgentWebParent(riskContent, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go("");
    }

    @Override
    public void initData() {
        ClickUtil.click(submit, view -> {
            if (!readCheck.isChecked()) {
                ToastUtils.show("请先阅读并同意风险告知书");
            } else {
                Verify1Activity.start(this);
                finish();
            }
        });
        mPresenter.getVeriryData("risk_agree");  //reg_agree
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, IntroductionActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        agentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        agentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        agentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    public void updateUi(String data) {
        agentWeb.getWebCreator().getWebView().loadDataWithBaseURL(BuildConfig.BASE_URL + "/", data, "text/html", "UTF-8", null);
    }
}
